package de.hrw.androidapp;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import de.hrw.androidapp.util.CanceledCoursesXMLParser;

public class CanceledCoursesScreenAdapter
extends BaseAdapter
{
	/**
	 * 
	 */
	private LayoutInflater mInflater;

	/**
	 * 
	 */
	private static List<String> canceledCourses = null;
	
	/**
	 * Constructor.
	 * 
	 * @param context
	 */
	public CanceledCoursesScreenAdapter(Context context) 
	{	
		mInflater = LayoutInflater.from(context);
		if (CanceledCoursesScreenAdapter.canceledCourses == null)
		{
			String canceledCoursesRSSUrl = ((BasicScreenActivity) context).getAppConfig().getProperty("canceled.courses.rssurl");
			CanceledCoursesXMLParser canceledCoursesXMLParser = new CanceledCoursesXMLParser(canceledCoursesRSSUrl);
			CanceledCoursesScreenAdapter.canceledCourses = canceledCoursesXMLParser.parse();
		}
	}
	
	/* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		return CanceledCoursesScreenAdapter.canceledCourses.size();
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int position) {
		return position;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		return position;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) 
		{
			convertView = mInflater.inflate(R.layout.ausfallendeveranstaltungeintrag, null);
			holder = new ViewHolder();
			holder.ausfallendeVeranstaltungTextView = (TextView) convertView.findViewById(R.id.ausfallendeVeranstaltungTextView);

			convertView.setTag(holder);
		}
		else 
		{
			holder = (ViewHolder) convertView.getTag();
		}
		
		String ausfallendeVeranstaltung = CanceledCoursesScreenAdapter.canceledCourses.get(position);
		
		holder.ausfallendeVeranstaltungTextView.setText(ausfallendeVeranstaltung);

		return convertView;
	}
	
	/**
	 * @author tvh-dev
	 *
	 */
	static class ViewHolder 
	{
		TextView ausfallendeVeranstaltungTextView;
	}
}
